<?php
#namespace cn\sihong\layout;

class template {
	public $dom;
	private $rootNamespace;
	private $xPath;
	private $title;
	public $headermenu;
	public $headermenuitem;
	public $footermenu;
	public $footermenuitem;
	private $language;
	public $content;
	
	public $leftmenu;
	public $leftmenuitem;
	public $warningmesage;
	public $warningmesageitem;

	public function __construct () {
		$this->dom = new DOMDocument ();
		$this->dom->formatOutput = true;
//		$this->language = new language();
	}

	public function load ($filename) {
		$this->dom->load ($filename);
		$this->xPath = new DOMXPath ($this->dom);
		$this->rootNamespace = $this->dom->lookupNamespaceUri ($this->dom->namespaceURI);
		if ($this->rootNamespace != '') {
			$this->xPath->registerNamespace ('xhtml', $this->rootNamespace);
		}
	}

	public function query ($expression, $contextnode = null) {
		return $this->xPath->query ($expression, $contextnode);
	}

	public function toHTML () {
		$html = $this->dom->saveXML ();
		$html = str_replace ('<?xml version="1.0"?>', '', $html);
		return $html;
	}

	public function initTitle () {
		$this->title = $this->query ('//xhtml:html/xhtml:head/xhtml:title')->item (0);
	}

	public function setTitle ($titletext) {
//		$this->title->nodeValue = $titletext;
		$this->title->nodeValue = htmlspecialchars ($titletext);
	}

	public function initMenu () {
		$this->initHeaderMenu ();
		$this->initfooterMenu ();
//		$this->initLeftMenu ();
		
	}

	public function initWarningMesage () {
		$this->warningmesage = $this->query ('//xhtml:html/xhtml:body/xhtml:div/xhtml:div[@id="page"]/xhtml:div[@id="content"]/xhtml:div[@id="warningmesage"]/xhtml:ul')->item (0);

		$this->warningmesage->parentNode->setAttribute ('style', 'display:none;visibility:hidden'); // Anthony

		$warningmesageitem = $this->query ('xhtml:li', $this->warningmesage)->item (0);
		$this->warningmesageitem = $warningmesageitem->cloneNode (true);
		$this->warningmesage->removeChild ($warningmesageitem);
//		var_dump ($this->warningmesage);
	}

	public function addWarningMesage ($warningmesagetext = 'Error') {
		$this->warningmesage->parentNode->setAttribute ('style', ''); // Anthony
		$warningmesageitem = $this->warningmesageitem->cloneNode (true);
		$warningmesageitem->nodeValue = htmlspecialchars ($warningmesagetext);
		$this->warningmesage->appendChild ($warningmesageitem);
//		var_dump ($this->warningmesage);
	}

	public function initContent () {
		$this->content = $this->query ('//xhtml:html/xhtml:body/xhtml:div/xhtml:div[@id="page"]/xhtml:div[@id="content"]')->item (0);
	}

	private function initHeaderMenu () {
	 $this->headermenu = $this->query ('//xhtml:html/xhtml:body/xhtml:div/xhtml:div[@id="primaryheader"]/xhtml:ul')->item (0);
		$headermenuitem = $this->query ('xhtml:li', $this->headermenu)->item (0);
//		var_dump($headermenuitem);
		$this->headermenuitem = $headermenuitem->cloneNode (true);
		$this->headermenu->removeChild ($headermenuitem);
	}
	
	private function initfooterMenu () {
		$this->footermenu = $this->query ('//xhtml:html/xhtml:body/xhtml:div/xhtml:div/xhtml:div/xhtml:div[@id="bottom"]/xhtml:ul')->item (0);
		$footermenuitem = $this->query ('xhtml:li', $this->footermenu)->item (0);
		$this->footermenuitem = $footermenuitem->cloneNode (true);
		$this->footermenu->removeChild ($footermenuitem);
	}

	private function setMenu () {
	}
}
?>